#include "pmswidget.h"
#include "ui_pmswidget.h"
#include "mainwindow.h"
#include "projectdatabaseoperations.h"
#include "customtabstyle.h"

extern int uniqueDeviceID;

PMSWidget::PMSWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::PMSWidget)
{
    ui->setupUi(this);
    
    error = new ErrorToDo;
    
    ui->tabWidget->tabBar()->setStyle(new CustomTabStyle);  //设置tabview的样式
    
    if(initDataFromProjectDB() < 0)
    {
        qCritical()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"从数据库载入接口对象表参数失败";
        error->showCriticalMsg(this, "从数据库载入接口对象表参数失败");
        return;
    }
    
    connect(ui->PMTimingSendTimeBox, SIGNAL(valueChanged(int)), this, SLOT(updatePMTimingSendTime(int)));
    connect(ui->CO2TimingSendTimeBox, SIGNAL(valueChanged(int)), this, SLOT(updateCO2TimingSendTime(int)));
    connect(ui->HumRhTimingSendTimeBox, SIGNAL(valueChanged(int)), this, SLOT(updateHumRhTimingSendTime(int)));
    connect(ui->PMBreakAtOnceSendTimeBox, SIGNAL(valueChanged(int)), this, SLOT(updatePMBreakAtOnceSendTime(int)));
    connect(ui->PMOnoffCommandBox, SIGNAL(valueChanged(int)), this, SLOT(updatePMOnoffCommand(int)));
    connect(ui->PMWorkModeBox, SIGNAL(valueChanged(int)), this, SLOT(updatePMWorkMode(int)));
    connect(ui->PMControlSetBox, SIGNAL(valueChanged(int)), this, SLOT(updatePMControlSet(int)));
    connect(ui->PMControlDataTypeBox, SIGNAL(valueChanged(int)), this, SLOT(updatePMControlDataType(int)));
    connect(ui->PMControlDataBox, SIGNAL(valueChanged(int)), this, SLOT(updatePMControlData(int)));
    connect(ui->CO2ControlThresholdBox, SIGNAL(valueChanged(int)), this, SLOT(updateCO2ControlThreshold(int)));
    connect(ui->CO2ControlDataTypeBox, SIGNAL(valueChanged(int)), this, SLOT(updateCO2ControlDataType(int)));
    connect(ui->CO2ControlDataBox, SIGNAL(valueChanged(int)), this, SLOT(updateCO2ControlData(int)));
    connect(ui->HumRhControlHighThresholdBox, SIGNAL(valueChanged(int)), this, SLOT(updateHumRhControlHighThreshold(int)));
    connect(ui->HumRhControlHighThresholdDataTypeBox, SIGNAL(valueChanged(int)), this, SLOT(updateHumRhControlHighThresholdDataType(int)));
    connect(ui->HumRhControlHighThresholdDataBox, SIGNAL(valueChanged(int)), this, SLOT(updateHumRhControlHighThresholdData(int)));
    connect(ui->HumRhControlLowThresholdBox, SIGNAL(valueChanged(int)), this, SLOT(updateHumRhControlLowThreshold(int)));
    connect(ui->HumRhControlLowThresholdDataTypeBox, SIGNAL(valueChanged(int)), this, SLOT(updateHumRhControlLowThresholdDataType(int)));
    connect(ui->HumRhControlLowThresholdDataBox, SIGNAL(valueChanged(int)), this, SLOT(updateHumRhControlLowThresholdData(int)));
}

PMSWidget::~PMSWidget()
{
    delete error;
    delete ui;
}

int PMSWidget::initDataFromProjectDB()
{
    int PMTimingSendTime = selectInterfaceObjectValueByDeviceIDAndInterfaceObjectNum(uniqueDeviceID, 0);
    if(PMTimingSendTime < 0)
    {
        qCritical()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"获取PM2p5定时发送时间失败";
        error->showCriticalMsg(this, "获取PM2p5定时发送时间失败");
        return PMTimingSendTime;
    }
    ui->PMTimingSendTimeBox->setValue(PMTimingSendTime);
    
    int CO2TimingSendTime = selectInterfaceObjectValueByDeviceIDAndInterfaceObjectNum(uniqueDeviceID, 1);
    if(CO2TimingSendTime < 0)
    {
        qCritical()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"获取CO2定时发送时间失败";
        error->showCriticalMsg(this, "获取CO2定时发送时间失败");
        return CO2TimingSendTime;
    }
    ui->CO2TimingSendTimeBox->setValue(CO2TimingSendTime);
    
    int HumRhTimingSendTime = selectInterfaceObjectValueByDeviceIDAndInterfaceObjectNum(uniqueDeviceID, 2);
    if(HumRhTimingSendTime < 0)
    {
        qCritical()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"获取HumRh定时发送时间失败";
        error->showCriticalMsg(this, "获取HumRh定时发送时间失败");
        return HumRhTimingSendTime;
    }
    ui->HumRhTimingSendTimeBox->setValue(HumRhTimingSendTime);
    
    int PMBreakAtOnceSend = selectInterfaceObjectValueByDeviceIDAndInterfaceObjectNum(uniqueDeviceID, 4);
    if(PMBreakAtOnceSend < 0)
    {
        qCritical()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"获取PM2p5突变立即发送值失败";
        error->showCriticalMsg(this, "获取PM2p5突变立即发送值失败");
        return PMBreakAtOnceSend;
    }
    ui->PMBreakAtOnceSendTimeBox->setValue(PMBreakAtOnceSend);
    
    int PMOnoffCommand = selectInterfaceObjectValueByDeviceIDAndInterfaceObjectNum(uniqueDeviceID, 5);
    if(PMOnoffCommand < 0)
    {
        qCritical()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"获取PM2p5开关指令值失败";
        error->showCriticalMsg(this, "获取PM2p5开关指令值失败");
        return PMOnoffCommand;
    }
    ui->PMOnoffCommandBox->setValue(PMOnoffCommand);
    
    int PMWorkMode = selectInterfaceObjectValueByDeviceIDAndInterfaceObjectNum(uniqueDeviceID, 6);
    if(PMWorkMode < 0)
    {
        qCritical()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"获取PM2p5传感工作模式值失败";
        error->showCriticalMsg(this, "获取PM2p5传感工作模式值失败");
        return PMWorkMode;
    }
    ui->PMWorkModeBox->setValue(PMWorkMode);
    
    int PMControlSet = selectInterfaceObjectValueByDeviceIDAndInterfaceObjectNum(uniqueDeviceID, 8);
    if(PMControlSet < 0)
    {
        qCritical()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"获取PM2.5控制设定值失败";
        error->showCriticalMsg(this, "获取PM2.5控制设定值失败");
        return PMControlSet;
    }
    ui->PMControlSetBox->setValue(PMControlSet);
    
    int PMControlDataType = selectInterfaceObjectValueByDeviceIDAndInterfaceObjectNum(uniqueDeviceID, 9);
    if(PMControlDataType < 0)
    {
        qCritical()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"获取PM2.5控制数据类型值失败";
        error->showCriticalMsg(this, "获取PM2.5控制数据类型值失败");
        return PMControlDataType;
    }
    ui->PMControlDataTypeBox->setValue(PMControlDataType);
    
    int PMControlData = selectInterfaceObjectValueByDeviceIDAndInterfaceObjectNum(uniqueDeviceID, 10);
    if(PMControlData < 0)
    {
        qCritical()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"获取PM2.5控制数据值失败";
        error->showCriticalMsg(this, "获取PM2.5控制数据值失败");
        return PMControlData;
    }
    ui->PMControlDataBox->setValue(PMControlData);
    
    int CO2ControlThreshold = selectInterfaceObjectValueByDeviceIDAndInterfaceObjectNum(uniqueDeviceID, 11);
    if(CO2ControlThreshold < 0)
    {
        qCritical()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"获取CO2控制门限值失败";
        error->showCriticalMsg(this, "获取CO2控制门限值失败");
        return CO2ControlThreshold;
    }
    ui->CO2ControlThresholdBox->setValue(CO2ControlThreshold);
    
    int CO2ControlDataType = selectInterfaceObjectValueByDeviceIDAndInterfaceObjectNum(uniqueDeviceID, 12);
    if(CO2ControlDataType < 0)
    {
        qCritical()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"获取CO2控制数据类型值失败";
        error->showCriticalMsg(this, "获取CO2控制数据类型值失败");
        return CO2ControlDataType;
    }
    ui->CO2ControlDataTypeBox->setValue(CO2ControlDataType);
    
    int CO2ControlData = selectInterfaceObjectValueByDeviceIDAndInterfaceObjectNum(uniqueDeviceID, 13);
    if(CO2ControlData < 0)
    {
        qCritical()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"获取CO2控制数据值失败";
        error->showCriticalMsg(this, "获取CO2控制数据值失败");
        return CO2ControlData;
    }
    ui->CO2ControlDataBox->setValue(CO2ControlData);
    
    int HumRhControlHighThreshold = selectInterfaceObjectValueByDeviceIDAndInterfaceObjectNum(uniqueDeviceID, 14);
    if(HumRhControlHighThreshold < 0)
    {
        qCritical()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"获取HumRh控制高门限值失败";
        error->showCriticalMsg(this, "获取HumRh控制高门限值失败");
        return HumRhControlHighThreshold;
    }
    ui->HumRhControlHighThresholdBox->setValue(HumRhControlHighThreshold);
    
    int HumRhControlHighThresholdDataType = selectInterfaceObjectValueByDeviceIDAndInterfaceObjectNum(uniqueDeviceID, 15);
    if(HumRhControlHighThresholdDataType < 0)
    {
        qCritical()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"获取HumRh控制高门限数据类型值失败";
        error->showCriticalMsg(this, "获取HumRh控制高门限数据类型值失败");
        return HumRhControlHighThresholdDataType;
    }
    ui->HumRhControlHighThresholdDataTypeBox->setValue(HumRhControlHighThresholdDataType);
    
    int HumRhControlHighThresholdData = selectInterfaceObjectValueByDeviceIDAndInterfaceObjectNum(uniqueDeviceID, 16);
    if(HumRhControlHighThresholdData < 0)
    {
        qCritical()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"获取HumRh控制高门限数据值失败";
        error->showCriticalMsg(this, "获取HumRh控制高门限数据值失败");
        return HumRhControlHighThresholdData;
    }
    ui->HumRhControlHighThresholdDataBox->setValue(HumRhControlHighThresholdData);
    
    int HumRhControlLowThreshold = selectInterfaceObjectValueByDeviceIDAndInterfaceObjectNum(uniqueDeviceID, 17);
    if(HumRhControlLowThreshold < 0)
    {
        qCritical()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"获取HumRh控制低门限值失败";
        error->showCriticalMsg(this, "获取HumRh控制低门限值失败");
        return HumRhControlLowThreshold;
    }
    ui->HumRhControlLowThresholdBox->setValue(HumRhControlLowThreshold);
    
    int HumRhControlLowThresholdDataType = selectInterfaceObjectValueByDeviceIDAndInterfaceObjectNum(uniqueDeviceID, 18);
    if(HumRhControlLowThresholdDataType < 0)
    {
        qCritical()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"获取HumRh控制低门限数据类型值失败";
        error->showCriticalMsg(this, "获取HumRh控制低门限数据类型值失败");
        return HumRhControlLowThresholdDataType;
    }
    ui->HumRhControlLowThresholdDataTypeBox->setValue(HumRhControlLowThresholdDataType);
    
    int HumRhControlLowThresholdData = selectInterfaceObjectValueByDeviceIDAndInterfaceObjectNum(uniqueDeviceID, 19);
    if(HumRhControlLowThresholdData < 0)
    {
        qCritical()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"获取HumRh控制低门限数据值失败";
        error->showCriticalMsg(this, "获取HumRh控制低门限数据值失败");
        return HumRhControlLowThresholdData;
    }
    ui->HumRhControlLowThresholdDataBox->setValue(HumRhControlLowThresholdData);
    
    return 1;
}

void PMSWidget::updatePMTimingSendTime(int value)
{
    qDebug()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"value:"<<value;
    if(!updateInterfaceValueByDeviceIDAndInterfaceObjectNum(uniqueDeviceID, 0, value))
    {
        qCritical()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"更新接口对象数据失败";
        error->showCriticalMsg(this, "更新接口对象数据失败");
        return;
    }
    qDebug()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"更新接口对象数据成功";
}

void PMSWidget::updateCO2TimingSendTime(int value)
{
    qDebug()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"value:"<<value;
    if(!updateInterfaceValueByDeviceIDAndInterfaceObjectNum(uniqueDeviceID, 1, value))
    {
        qCritical()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"更新接口对象数据失败";
        error->showCriticalMsg(this, "更新接口对象数据失败");
        return;
    }
    qDebug()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"更新接口对象数据成功";
}

void PMSWidget::updateHumRhTimingSendTime(int value)
{
    qDebug()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"value:"<<value;
    if(!updateInterfaceValueByDeviceIDAndInterfaceObjectNum(uniqueDeviceID, 2, value))
    {
        qCritical()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"更新接口对象数据失败";
        error->showCriticalMsg(this, "更新接口对象数据失败");
        return;
    }
    qDebug()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"更新接口对象数据成功";
}

void PMSWidget::updatePMBreakAtOnceSendTime(int value)
{
    qDebug()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"value:"<<value;
    if(!updateInterfaceValueByDeviceIDAndInterfaceObjectNum(uniqueDeviceID, 4, value))
    {
        qCritical()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"更新接口对象数据失败";
        error->showCriticalMsg(this, "更新接口对象数据失败");
        return;
    }
    qDebug()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"更新接口对象数据成功";
}

void PMSWidget::updatePMOnoffCommand(int value)
{
    qDebug()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"value:"<<value;
    if(!updateInterfaceValueByDeviceIDAndInterfaceObjectNum(uniqueDeviceID, 5, value))
    {
        qCritical()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"更新接口对象数据失败";
        error->showCriticalMsg(this, "更新接口对象数据失败");
        return;
    }
    qDebug()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"更新接口对象数据成功";
}

void PMSWidget::updatePMWorkMode(int value)
{
    qDebug()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"value:"<<value;
    if(!updateInterfaceValueByDeviceIDAndInterfaceObjectNum(uniqueDeviceID, 6, value))
    {
        qCritical()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"更新接口对象数据失败";
        error->showCriticalMsg(this, "更新接口对象数据失败");
        return;
    }
    qDebug()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"更新接口对象数据成功";
}

void PMSWidget::updatePMControlSet(int value)
{
    qDebug()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"value:"<<value;
    if(!updateInterfaceValueByDeviceIDAndInterfaceObjectNum(uniqueDeviceID, 8, value))
    {
        qCritical()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"更新接口对象数据失败";
        error->showCriticalMsg(this, "更新接口对象数据失败");
        return;
    }
    qDebug()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"更新接口对象数据成功";
}

void PMSWidget::updatePMControlDataType(int value)
{
    qDebug()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"value:"<<value;
    if(!updateInterfaceValueByDeviceIDAndInterfaceObjectNum(uniqueDeviceID, 9, value))
    {
        qCritical()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"更新接口对象数据失败";
        error->showCriticalMsg(this, "更新接口对象数据失败");
        return;
    }
    qDebug()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"更新接口对象数据成功";
}

void PMSWidget::updatePMControlData(int value)
{
    qDebug()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"value:"<<value;
    if(!updateInterfaceValueByDeviceIDAndInterfaceObjectNum(uniqueDeviceID, 10, value))
    {
        qCritical()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"更新接口对象数据失败";
        error->showCriticalMsg(this, "更新接口对象数据失败");
        return;
    }
    qDebug()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"更新接口对象数据成功";
}

void PMSWidget::updateCO2ControlThreshold(int value)
{
    qDebug()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"value:"<<value;
    if(!updateInterfaceValueByDeviceIDAndInterfaceObjectNum(uniqueDeviceID, 11, value))
    {
        qCritical()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"更新接口对象数据失败";
        error->showCriticalMsg(this, "更新接口对象数据失败");
        return;
    }
    qDebug()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"更新接口对象数据成功";
}

void PMSWidget::updateCO2ControlDataType(int value)
{
    qDebug()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"value:"<<value;
    if(!updateInterfaceValueByDeviceIDAndInterfaceObjectNum(uniqueDeviceID, 12, value))
    {
        qCritical()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"更新接口对象数据失败";
        error->showCriticalMsg(this, "更新接口对象数据失败");
        return;
    }
    qDebug()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"更新接口对象数据成功";
}

void PMSWidget::updateCO2ControlData(int value)
{
    qDebug()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"value:"<<value;
    if(!updateInterfaceValueByDeviceIDAndInterfaceObjectNum(uniqueDeviceID, 13, value))
    {
        qCritical()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"更新接口对象数据失败";
        error->showCriticalMsg(this, "更新接口对象数据失败");
        return;
    }
    qDebug()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"更新接口对象数据成功";
}

void PMSWidget::updateHumRhControlHighThreshold(int value)
{
    qDebug()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"value:"<<value;
    if(!updateInterfaceValueByDeviceIDAndInterfaceObjectNum(uniqueDeviceID, 14, value))
    {
        qCritical()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"更新接口对象数据失败";
        error->showCriticalMsg(this, "更新接口对象数据失败");
        return;
    }
    qDebug()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"更新接口对象数据成功";
}

void PMSWidget::updateHumRhControlHighThresholdDataType(int value)
{
    qDebug()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"value:"<<value;
    if(!updateInterfaceValueByDeviceIDAndInterfaceObjectNum(uniqueDeviceID, 15, value))
    {
        qCritical()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"更新接口对象数据失败";
        error->showCriticalMsg(this, "更新接口对象数据失败");
        return;
    }
    qDebug()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"更新接口对象数据成功";
}

void PMSWidget::updateHumRhControlHighThresholdData(int value)
{
    qDebug()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"value:"<<value;
    if(!updateInterfaceValueByDeviceIDAndInterfaceObjectNum(uniqueDeviceID, 16, value))
    {
        qCritical()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"更新接口对象数据失败";
        error->showCriticalMsg(this, "更新接口对象数据失败");
        return;
    }
    qDebug()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"更新接口对象数据成功";
}

void PMSWidget::updateHumRhControlLowThreshold(int value)
{
    qDebug()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"value:"<<value;
    if(!updateInterfaceValueByDeviceIDAndInterfaceObjectNum(uniqueDeviceID, 17, value))
    {
        qCritical()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"更新接口对象数据失败";
        error->showCriticalMsg(this, "更新接口对象数据失败");
        return;
    }
    qDebug()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"更新接口对象数据成功";
}

void PMSWidget::updateHumRhControlLowThresholdDataType(int value)
{
    qDebug()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"value:"<<value;
    if(!updateInterfaceValueByDeviceIDAndInterfaceObjectNum(uniqueDeviceID, 18, value))
    {
        qCritical()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"更新接口对象数据失败";
        error->showCriticalMsg(this, "更新接口对象数据失败");
        return;
    }
    qDebug()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"更新接口对象数据成功";
}

void PMSWidget::updateHumRhControlLowThresholdData(int value)
{
    qDebug()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"value:"<<value;
    if(!updateInterfaceValueByDeviceIDAndInterfaceObjectNum(uniqueDeviceID, 19, value))
    {
        qCritical()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"更新接口对象数据失败";
        error->showCriticalMsg(this, "更新接口对象数据失败");
        return;
    }
    qDebug()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"更新接口对象数据成功";
}
